<template>
  <div class="tar_bar_item" @click="sonclick">
    <!-- 1.slot插槽中不能动态绑定样式，因为在运行的时候slot的内容会被替换掉，所以设置样式没有用   -->
    <!-- <slot name="item-png" v-if="isActive"></slot>
    <slot name="item-png1" v-else></slot>
    <slot :class="{ active: isActive }" name="item-text"></slot> -->

    <div v-if="changeIsActive"><slot name="item-png"></slot></div>
    <div v-else><slot name="item-png1"></slot></div>
    <div :style="textColor">
      <!-- 2.动态绑定class也可以，但是要在style中设置样式
      <div :class="{active:changeIsActive}"> -->
      <slot name="item-text"></slot>
    </div>
  </div>
</template>
<script>
export default {
  name: "tarbar_item",
  data() {
    return {
      // isActive: false,
    };
  },
  computed: {
    changeIsActive() {
      return this.$route.path.indexOf(this.path) != -1;
    },
    textColor() {
      return this.changeIsActive ? { color: this.colorr } : {};
    },
  },
  methods: {
    sonclick() {
      // 解决重复点击一个会报错的问题
      if (this.$route.path != this.path) {
        this.$router.push(this.path);
      }
      // console.log(this.$route);
      // console.log(this.$router);
    },
  },
  props: {
    path: String,
    colorr: String,
  },
};
</script>
<style>
.tar_bar_item {
  flex: 1;
  height: 49px;
  text-align: center;
  width: 100%;
  font-size: 9px;
}
.tar_bar_item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  vertical-align: middle;
  /* vertical-align: middle;缩短图片与文字的距离 */
}

/* 2. .active {
  color: red;
} */
</style>
